package com.pdf.demo.util;


import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

/**
 * 正常范围实体类
 *
 * @author wzk
 */
public class NormalRangeUtil {


    /**
     * 根据身高获取标准体重
     * 计算公式 身高²*BMI范围
     *
     * @param height
     * @return
     */
    public static String weightRange(Object height) {
        if (null == height) {
            return "-";
        }
        if (StringUtils.isEmpty(height.toString())) {
            return "-";
        }
        double heightD = Double.parseDouble(height.toString());
        BigDecimal one = new BigDecimal((heightD - 105) * 0.9).setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal two = new BigDecimal((heightD - 105) * 1.1).setScale(2, BigDecimal.ROUND_HALF_UP);

        return one + "-" + two;
//        BigDecimal square = new BigDecimal(height.toString()).pow(2).divide(new BigDecimal("10000"));
//        return square.multiply(new BigDecimal(18.5)).setScale(2, BigDecimal.ROUND_HALF_UP)
//                + "-" + square.multiply(new BigDecimal(23.9)).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 脂肪率标准
     *
     * @param age
     * @param sex
     * @return
     */
    public static String fatRateRange(Object age, Object sex) {
        if (null == age || null == sex) {
            return "-";
        }
        if (StringUtils.isEmpty(age.toString()) || StringUtils.isEmpty(sex.toString())) {
            return "-";
        }
        String sexStr = String.valueOf(new Double(sex.toString()).intValue());
        if (StringUtils.equals("0", sexStr)) {
            return "-";
        }
        Integer ageInt = new Double(age.toString()).intValue();
        switch (sexStr) {
            case "1":

                if (ageInt <= 39) {
                    return "18-23";
                }
                if (ageInt >= 40 && ageInt < 55) {
                    return "19-24";
                }
                if (ageInt >= 55) {
                    return "20-25";
                }
                break;
            case "2":
                if (ageInt <= 39) {
                    return "21-25";
                }
                if (ageInt >= 40 && ageInt < 55) {
                    return "22-26";
                }
                if (ageInt >= 55) {
                    return "23-27";
                }
                break;
        }
        return "-";
    }

    public static String fatRange(Object age, Object sex, Object width) {
        String range = fatRateRange(age, sex);
        if (StringUtils.equals("-", range)) {
            return "-";
        }
        if (null == width || StringUtils.isEmpty(width.toString())) {
            return "-";
        }
        String[] ranges = range.split("-");
        BigDecimal bigDecimal = new BigDecimal(width.toString());
        if (bigDecimal.compareTo(new BigDecimal(0)) == 0) {
            return "-";
        }
        return bigDecimal.multiply(new BigDecimal(ranges[0])).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP)
                + "-"
                + bigDecimal.multiply(new BigDecimal(ranges[1])).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 水分率
     *
     * @param sex
     * @return
     */
    public static String waterRateRange(Object sex) {
        if (null == sex) {
            return "-";
        }
        if (StringUtils.isEmpty(sex.toString())) {
            return "-";
        }
        String sexStr = String.valueOf(new Double(sex.toString()).intValue());
        if (StringUtils.equals("0", sexStr)) {
            return "-";
        }
        if (StringUtils.equals("1", sexStr)) {
            return "50-65";
        } else {
            return "45-55";
        }
    }

    /**
     * 水分量
     *
     * @param sex
     * @param width
     * @return
     */
    public static String waterRange(Object sex, Object width) {
        String range = waterRateRange(sex);
        if (StringUtils.equals("-", range)) {
            return "-";
        }
        if (null == width || StringUtils.isEmpty(width.toString())) {
            return "-";
        }
        String[] ranges = range.split("-");
        BigDecimal bigDecimal = new BigDecimal(width.toString());
        if (bigDecimal.compareTo(new BigDecimal(0)) == 0) {
            return "-";
        }
        return bigDecimal.multiply(new BigDecimal(ranges[0])).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP)
                + "-"
                + bigDecimal.multiply(new BigDecimal(ranges[1])).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
    }

    public static String basalMetabolismRange(Object sex, Object age) {
        if (null == age || null == sex) {
            return "-";
        }
        if (StringUtils.isEmpty(age.toString()) || StringUtils.isEmpty(sex.toString())) {
            return "-";
        }
        String sexStr = String.valueOf(new Double(sex.toString()).intValue());
        if (StringUtils.equals("0", sexStr)) {
            return "-";
        }
        Integer ageInt = new Double(age.toString()).intValue();
        switch (sexStr) {
            case "1":
                if (ageInt < 30) {
                    return "1395-1705";
                }
                if (ageInt >= 30 && ageInt <= 49) {
                    return "1350-1650";
                }
                if (ageInt >= 50 && ageInt <= 70) {
                    return "1215-1485";
                }
                if (ageInt > 70) {
                    return "1098-1342";
                }
                break;
            case "2":
                if (ageInt < 30) {
                    return "1089-1331";
                }
                if (ageInt >= 30 && ageInt <= 49) {
                    return "1053-1287";
                }
                if (ageInt >= 50 && ageInt <= 70) {
                    return "999-1221";
                }
                if (ageInt > 70) {
                    return "909-1111";
                }
                break;
        }
        return "-";
    }

    public static String muscleRateRange(Object age, Object sex) {
        if (null == age || null == sex) {
            return "-";
        }
        if (StringUtils.isEmpty(age.toString()) || StringUtils.isEmpty(sex.toString())) {
            return "-";
        }
        String sexStr = String.valueOf(new Double(sex.toString()).intValue());
        if (StringUtils.equals("0", sexStr)) {
            return "-";
        }
        Integer ageInt = new Double(age.toString()).intValue();
        switch (sexStr) {
            case "1":
                if (ageInt < 15) {
                    return "44-57";
                }
                if (ageInt >= 15 && ageInt <= 19) {
                    return "43-56";
                }
                if (ageInt >= 20 && ageInt <= 29) {
                    return "42-55";
                }
                if (ageInt >= 30 && ageInt <= 39) {
                    return "41-54";
                }
                if (ageInt >= 40 && ageInt <= 49) {
                    return "40-50";
                }
                if (ageInt >= 50 && ageInt <= 59) {
                    return "39-48";
                }
                if (ageInt >= 60 && ageInt <= 69) {
                    return "38-47";
                }
                if (ageInt >= 70) {
                    return "37-46";
                }
                break;
            case "2":
                if (ageInt < 15) {
                    return "36-43";
                }
                if (ageInt >= 15 && ageInt <= 19) {
                    return "35-41";
                }
                if (ageInt >= 20 && ageInt <= 29) {
                    return "34-39";
                }
                if (ageInt >= 30 && ageInt <= 39) {
                    return "33-38";
                }
                if (ageInt >= 40 && ageInt <= 49) {
                    return "31-36";
                }
                if (ageInt >= 50 && ageInt <= 59) {
                    return "29-34";
                }
                if (ageInt >= 60 && ageInt <= 69) {
                    return "28-33";
                }
                if (ageInt >= 70) {
                    return "27-32";
                }
                break;
        }
        return "-";
    }

    public static String styleStatus(Object status) {
        if (null == status || StringUtils.isEmpty(status.toString())) {
            return "-";
        }
        String statusStr = String.valueOf(new BigDecimal(status.toString()).intValue());
        if (StringUtils.equals("3", statusStr)) {
            return "正常";
        }
        if (StringUtils.equals("1", statusStr)) {
            return styleStatus("偏高");
        }
        if (StringUtils.equals("2", statusStr)) {
            return styleStatus("偏低");
        }
        return "-";
    }

    public static String sexStyle(Object sexObj) {
        if (null == sexObj || StringUtils.isEmpty(sexObj.toString())) {
            return "未知";
        }
        String sex = String.valueOf(new Double(sexObj.toString()).intValue());
        switch (sex) {
            case "0":
                return "未知";
            case "1":
                return "男";
            case "2":
                return "女";
        }
        return "未知";
    }

    public static String hwBmiStateStyle(Object status) {
        if (null == status || StringUtils.isEmpty(status.toString())) {
            return "-";
        }
        String statusStr = String.valueOf(new BigDecimal(status.toString()).intValue());
        if ("1".equals(statusStr)) {
            return styleStatus("偏瘦");
        } else if ("2".equals(statusStr)) {
            return "正常";
        } else if ("3".equals(statusStr)) {
            return styleStatus("超重");
        } else if ("4".equals(statusStr)) {
            return styleStatus("肥胖");
        }
        return "-";
    }

    public static String pressureStatus(Object status) {
        if (null == status || StringUtils.isEmpty(status.toString())) {
            return "-";
        }
        String statusStr = String.valueOf(new BigDecimal(status.toString()).intValue());
        if (StringUtils.equals("5", statusStr)) {
            return "正常";
        }
        if (StringUtils.equals("1", statusStr)) {
            return styleStatus("低压");
        }
        if (StringUtils.equals("2", statusStr)) {
            return styleStatus("轻度高血压");
        }
        if (StringUtils.equals("3", statusStr)) {
            return styleStatus("中度高血压");
        }
        if (StringUtils.equals("4", statusStr)) {
            return styleStatus("重度高血压");
        }
        return "-";
    }

    public static String bmiAdvice(Object status) {
        if (null == status || StringUtils.isEmpty(status.toString())) {
            return "";
        }
        String statusStr = String.valueOf(new BigDecimal(status.toString()).intValue());
        if (StringUtils.equals("1", statusStr)) {
            return "<p>饮食：饮食宜荤素搭配，种类多样，不偏食，不挑食。适当增加牛奶、鸡蛋、豆及其制品、 鱼虾贝类优质蛋白的摄入。</p>" +
                    "<p>运动：加强锻炼，提高身体素质，改善神经、血管的调节功能，促进血液循环。每周运动三 次以上，每次运动30分钟以上</p>" +
                    "<p>生活习惯：建立良好的生活习惯，保持良好的精神状态；保证充足的睡眠。</p>";
        }
        if (StringUtils.equals("3", statusStr) || StringUtils.equals("4", statusStr)) {
            return "<p>饮食：控制总能量的摄入；膳食清淡少盐少油；荤素搭配，均衡饮食；尽量采用凉拌、蒸、 煮、炖、焖的烹调方式。</p>" +
                    "<p>运动：加强锻炼，增加能量的消耗，促进脂肪的分解。运动以中等负荷强度，即以心率计 110—130次/分，时间持续在30分钟以上，慢走、快跑、自行车、游泳等有氧运动。</p>" +
                    "<p>生活习惯：建立良好的生活习惯，劳逸结合，避免过度疲劳；保持良好的精神状态；保证充 足的睡眠。</p>";
        }
        return "";
    }

    public static String fatRateAdvice(Object status) {
        if (null == status || StringUtils.isEmpty(status.toString())) {
            return "";
        }
        String statusStr = String.valueOf(new BigDecimal(status.toString()).intValue());
        if (StringUtils.equals("1", statusStr)) {
            return "<p>脂肪率偏高</p>" +
                    "<p>饮食：控制总能量的摄入；膳食清淡少盐少油；荤素搭配，均衡饮食；尽量采用凉拌、蒸、煮、炖、焖的烹调方式。</p>" +
                    "<p>运动：加强锻炼，增加能量的消耗，促进脂肪的分解。运动以中等负荷强度，即以心率计110—130次/分时间持续在30分钟以上，慢走、 快跑、自行车、游泳等有氧运动。</p>" +
                    "<p>生活习惯：改变不吃早餐，晚餐过晚，一次性进食过多，进餐速度过快等不良习惯。纠正爱吃零食、甜食和高油脂食品的习惯。</p>";
        }
        if (StringUtils.equals("2", statusStr)) {
            return "<p>脂肪率偏低</p>" +
                    "<p>饮食：饮食宜全面均衡，荤素搭配，种类多样，适当增加牛奶、蛋、豆及其制品、鱼虾贝类优质蛋白的摄入。</p>" +
                    "<p>运动：加强锻炼，提高身体素质改善神经、血管的调节功能，促进血液循环。每周运动3-5 次，每次运动30分钟以上。</p>" +
                    "<p>生活习惯：建立良好的生活习惯，保持良好的精神状态；保证充足的睡眠。</p>";
        }
        return "";
    }


    private static String styleStatus(String name) {
        return "<span style=\"color: red\">" + name + "</span>";
    }
}
